﻿using System.Diagnostics;

namespace RealidadeAumentada.Entidades
{
    public class EHistograma
    {
	    public int[] data;
        public int length;
	    public int total_of_data;    
 
        #region Construtor
        public EHistograma(int Tamanho)
        {
            this.data = new int[Tamanho];
            this.length = Tamanho;
            this.total_of_data = 0;
        }
        #endregion

        #region Total
        public int ObterTotal(int i_st, int i_ed)
        {
            Debug.Assert(i_st < i_ed && i_ed < this.length);
            int result = 0;
            int[] s = this.data;
            for (int i = i_st; i <= i_ed; i++)
            {
                result += s[i];
            }
            return result;
        }
        #endregion

        #region Corte
        public void CorteBaixo(int i_pos)
        {
            int s = 0;
            for (int i = 0; i < i_pos; i++)
            {
                s += this.data[i];
                this.data[i] = 0;
            }
            this.total_of_data -= s;
        }

        public void CorteAlto(int i_pos)
        {
            int s = 0;
            for (int i = this.length - 1; i >= i_pos; i--)
            {
                s += this.data[i];
                this.data[i] = 0;
            }
            this.total_of_data -= s;
        }
        #endregion

        #region Minimo
        public int ObterAmostraMinima()
        {
            int[] data = this.data;
            int ret = this.length - 1;
            int min = data[ret];
            for (int i = this.length - 2; i >= 0; i--)
            {
                if (data[i] < min)
                {
                    min = data[i];
                    ret = i;
                }
            }
            return ret;
        }

        public int ObterDadosMinimos()
        {
            return this.data[this.ObterAmostraMinima()];
        }
        #endregion

        #region Media
        public int ObterMedia()
        {
            long sum = 0;
            for (int i = this.length - 1; i >= 0; i--)
            {
                sum += this.data[i] * i;
            }
            return (int)(sum / this.total_of_data);
        }
        #endregion       
    }
}
